#!/usr/bin/env bash

ironruby_install()
{
  rvm_error "WARNING! IronRuby is not anywhere around usable (with or without RVM),
there are multiple issues with it, here is an effort to improve/fix it:
    https://github.com/rvm/rvm/pull/1543"

  builtin command -v mono > /dev/null ||
  {
    printf "%b" "mono must be installed and in your path in order to install IronRuby."
    return 1
  }
  if
    (( ${rvm_head_flag:=0} == 1 ))
  then
    mono_version="$(mono -V | \command \head -n 1 | __rvm_grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | \command \head -n 1 )"
    if
      [[ -n "${mono_version:-}" ]]
    then
      mono_major_ver="$(echo "$mono_version" | cut -d '.' -f1)"
      mono_minor_ver="$(echo "$mono_version" | cut -d '.' -f2)"
      if
        [[ $mono_major_ver -lt 2 ]] ||
        [[ $mono_major_ver -eq 2 && $mono_minor_ver -lt 6 ]]
      then
        printf "%b" "Mono 2.6 (or greater) must be installed and in your path in order to build IronRuby from the repository."
        printf "%b" "Version detected: ${mono_version}"
        return 1
      fi
    else
      printf "%b" "Cannot recognize mono version."
      return 1
    fi

    __rvm_ensure_has_mri_ruby

    __rvm_fetch_ruby || return $?

    __rvm_cd "${rvm_src_path}/$rvm_ruby_string"

    compatible_ruby="$(__rvm_mri_ruby)"

    "$rvm_wrappers_path/$compatible_ruby/gem" install pathname2 --no-rdoc --no-ri

    # MONO_LIB=/Library/Frameworks/Mono.framework/Versions/current/lib/
    rvm_ruby_make=( $rvm_wrappers_path/$compatible_ruby/rake MERLIN_ROOT="${rvm_src_path}/$rvm_ruby_string/Merlin/Main" compile mono=1 configuration=release --trace )

    __rvm_log_command rake "Building IronRuby..." "${rvm_ruby_make[@]}" || return $?

    __rvm_rm_rf "$rvm_ruby_home"/*

    mkdir -p "$rvm_ruby_home/bin" "$rvm_ruby_home/lib" "$rvm_ruby_home/Lib/ruby" "$rvm_ruby_home/Lib/IronRuby"

    __rvm_cp -r "${rvm_src_path}/$rvm_ruby_string/Merlin/Main/Bin/mono_release"/* "$rvm_ruby_home/bin/"
    __rvm_cp -r "${rvm_src_path}/$rvm_ruby_string/Merlin/Main/Languages/Ruby/Scripts/bin"/* "$rvm_ruby_home/bin/"
    __rvm_cp -r "${rvm_src_path}/$rvm_ruby_string/Merlin/External.LCA_RESTRICTED/Languages/Ruby/redist-libs/ruby"/* "$rvm_ruby_home/lib/ruby"
    __rvm_cp -r "${rvm_src_path}/$rvm_ruby_string/Merlin/Main/Languages/Ruby/Libs"/* "$rvm_ruby_home/lib/IronRuby"
  else
    rvm_log "Retrieving IronRuby"

    "$rvm_scripts_path/fetch" "$rvm_ruby_url" "$rvm_ruby_package_file" ||
    {
      result=$?
      rvm_error "There has been an error while trying to fetch the source. \nHalting the installation."
      exit $result
    }

    mkdir -p "${rvm_src_path}/$rvm_ruby_string" "$rvm_ruby_home"

    __rvm_log_command "extract" "$rvm_ruby_string - #extracting $rvm_ruby_package_file to ${rvm_src_path}/$rvm_ruby_string" \
      __rvm_package_extract "${rvm_archives_path}/${rvm_ruby_package_file}" "${rvm_src_path}/$rvm_ruby_string" ||
    case $? in
      199)
        rvm_error "\nUnrecognized archive format '$archive_format'"
        return 199
        ;;
      *)
        rvm_error "There has been an error while trying to extract the source. Halting the installation."
        return 1
        ;;
    esac

    for dir in bin Lib Silverlight
    do
      __rvm_cp -Rf "${rvm_src_path}/$rvm_ruby_string/$dir" "$rvm_ruby_home/$dir"
    done
  fi

  binaries=(gem irb rdoc rake ri ruby)

  for binary_name in "${binaries[@]}"
  do
    if
      [[ -s "$rvm_ruby_home/bin/$binary_name" ]]
    then
      \command \tr -d '\r' < "$rvm_ruby_home/bin/$binary_name" > "$rvm_ruby_home/bin/$binary_name.new" &&
      \command \mv -f "$rvm_ruby_home/bin/$binary_name.new" "$rvm_ruby_home/bin/$binary_name"
      chmod +x "$rvm_ruby_home/bin/$binary_name"
    fi
  done
  unset binaries

  if
    [[ -f "$rvm_ruby_home/bin/ir" ]]
  then
    __rvm_sed -e '1,1s=.*=#!'"/usr/bin/env bash=" "$rvm_ruby_home/bin/ir" | \command \tr -d '\r' > "$rvm_ruby_home/bin/ir.new" &&
    \command \mv -f "$rvm_ruby_home/bin/ir.new" "$rvm_ruby_home/bin/ir"
  else
    echo "mono \"$rvm_ruby_home/bin/ir\" \"\$@\"" > "$rvm_ruby_home/bin/ir"
  fi

  chmod +x "$rvm_ruby_home/bin/ir"

  ln -fs "$rvm_ruby_home/bin/ir" "$rvm_ruby_home/bin/ruby"

  __rvm_initial_gemsets_create "$rvm_ruby_home/bin/ruby"
  __rvm_fetch_ruby_cleanup
}
